﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.System;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.BaseData;

namespace ZBJF.Warranty.AdminSite.Controllers
{
    public class D_CityController : Controller
    {

        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public JsonResult Index(D_CityModel model)
        {
            var total = 0;
            var list = ServiceFactory.D_CityServiceInstance.GetPagingList(model, out total).AsNoTracking().ToList().Select(o => new
            {
                ProvinceName = o.D_Province != null ? o.D_Province.ProvinceName : string.Empty,
                o.CityID,
                o.CityName,
                o.Sort,
                o.Hot,
                o.Letter,
                o.ZipCode,
                o.IsAvailable
            });
            return Json(new
            {
                sEcho = model.sEcho,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = list
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Edit(int id)
        {
            var model = ServiceFactory.D_CityServiceInstance.LoadByID(id);
            return View(model);
        }

        public ActionResult SaveCity(D_City model)
        {
            var responseResult = new ResponseResult
            {
                OpreateType = model.CityID > 0 ? OpreateType.Update : OpreateType.Add
            };
            var serv = ServiceFactory.D_CityServiceInstance;
            if (model.CityID < 1)
            {
                if (serv.IsExist(model.ProvinceID, model.CityName))
                {
                    responseResult.Message = "该城市已经存在!";
                }
                else
                {
                    var entity = serv.AddEntities(model);
                    responseResult.Success = entity.CityID > 0;
                }
            }
            else
            {
                var entity = serv.LoadByID(model.CityID);
                if (entity != null)
                {
                    if (serv.IsExist(model.ProvinceID, model.CityName, model.CityID))
                    {
                        responseResult.Message = "已存在同名的城市，请修改城市名!";
                    }
                    entity.CityName = model.CityName;
                    entity.Letter = model.Letter;
                    entity.ZipCode = model.ZipCode;
                    entity.Hot = model.Hot;
                    entity.ProvinceID = model.ProvinceID;
                    responseResult.Success = serv.UpdateEntities(entity);
                }
                else
                {
                    responseResult.Message = "该城市不存在或已被删除";
                }
            }
            TempData["ResponseResult"] = responseResult;
            return responseResult.OpreateType == OpreateType.Add ? RedirectToAction("Index") : RedirectToAction("Edit", new { id = model.CityID });
        }
       
    }
}